'use strict';

var app = new Vue({
    el: '#app',
    data: {
        keyword: '', //搜索关键词
        placeholder: '', //搜索提示
        searchResult:[],
        sort1: [{
            text: '首页',
            type: 0,
            list:[]
        }],
        chooseIndex: 0, //分类选择的序列号
        home: { //首页
            //轮播图1
            banner_imgs: [],
            //本月新品
            swiper_view: [],
            //热卖单品
            hot_goods: [],
            //美妆护肤
            makeups: [{
                img: '/Static/mobile/index/images/makeups_01.jpg',
                goods_name: '娇霏草本植物修护洗护套装', //商品名称
                desc1: '天然植物 自然萃取 | 两瓶装39.90元',
                desc2: '(仅可单独购买，预售期不支持退款)',
                buy_num: 3677, //已购买人数
                price: '39.90', //活动价
                market_pri: '399.90' //市场价
            }, {
                img: '/Static/mobile/index/images/makeups_02.jpg',
                goods_name: '娇霏草本植物修护洗护套装', //商品名称
                desc1: '天然植物 自然萃取 | 两瓶装39.90元',
                desc2: '(仅可单独购买，预售期不支持退款)',
                buy_num: 666, //已购买人数
                price: '19.90', //活动价
                market_pri: '199.90' //市场价
            }, {
                img: '/Static/mobile/index/images/makeups_03.jpg',
                goods_name: '娇霏草本植物修护洗护套装', //商品名称
                desc1: '天然植物 自然萃取 | 两瓶装39.90元',
                desc2: '(仅可单独购买，预售期不支持退款)',
                buy_num: 888, //已购买人数
                price: '29.90', //活动价
                market_pri: '499.90' //市场价
            }]
        }
    },
    componenets: {},
    created: function () {
        var self = this;
        $.get('/mobile/index/apiInitIndex').then(function (res) {
            $('#app').show();
            if(res.ret==0) {
                var data = res.data;
                data.swiperList.forEach(function (item) {
                    self.home.banner_imgs.push({
                        img: item.pic,
                        url: item.link
                    })
                });
                data.monthList.forEach(function (item) {
                    self.home.swiper_view.push({
                        img: item.imgurl,
                        url: item.url
                    })
                });
                data.hotList.forEach(function (item) {
                    self.home.hot_goods.push(item);
                });
                data.typeList.forEach(function (item) {
                    item['list'] = [];
                    self.sort1.push(item);
                })
            }
        })
    },
    computed: {
        isSearch: function () {
            if(this.keyword==='') {
                return false;
            }
            return true;
        }
    },
    watch: {
        chooseIndex: function (val) {
            if(val!=0) {
                var typeItem = this.sort1[val];
                if(typeItem.list.length==0) {
                    $.get('/mobile/index/apiGetGoodsList?type='+typeItem.type).then(function(res) {
                        if(res.ret==0) {
                            res.data.forEach(function(item){
                                typeItem.list.push(item);
                            })
                        }
                    });
                }
            }
        }
    },
    methods: {
        //关键词搜索
        onSearch: function onSearch() {
            console.log(1111);
        },
        //改变分类
        changeSort: function changeSort(index, title) {
            this.chooseIndex = index;
        },
        checkForm: function () {
            var self = this;
            $.get('/mobile/index/apiGetGoodsList?key='+this.keyword).then(function(res) {
                if(res.ret==0) {
                    res.data.forEach(function(item){
                        self.searchResult.push(item);
                    })
                }
            });
        }
    }

});